//
//  ICTransferCustomerView.m
//  ICome
//
//  Created by zhangrongwu on 2016/12/1.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICTransferCustomerView.h"
#import "ICServerModel.h"
#import "ICHeaderFile.h"

#define CustomerPickerViewHeight 213
@interface ICTransferCustomerView()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong)UIPickerView *customerPicker;
@property (nonatomic, strong)UILabel *headView;
@property (nonatomic, strong)UIButton *cancelButton;
@property (nonatomic, strong)UIButton *sureAction;
@property (nonatomic, assign)NSInteger currDateRow;
@property (nonatomic, strong)UIView *superView;

@end

@implementation ICTransferCustomerView

-(instancetype)initWithCustomers:(NSMutableArray *)customers superView:(UIView *)superView {
    if (self = [super init]) {
        self.width = superView.width;
        self.height = 260;
        self.frame = CGRectMake(0, superView.height, self.width, self.height);
        self.pickerArray = customers;
        [self setUI];
        self.superView = superView;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setUI {
    self.customerPicker.backgroundColor = [UIColor whiteColor];
    self.headView.backgroundColor = ICRGB(0xf6f6f8);
    [self sureAction];
    [self cancelButton];
}

-(void)show {
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(0, self.superView.height - self.height, self.width, self.height);
    }];
}

-(void)hide {
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(0, self.superView.height, self.width, self.height);
    }];
}

-(void)setPickerArray:(NSMutableArray *)pickerArray {
    _pickerArray = pickerArray;
    self.currDateRow = 0;
    [self.customerPicker reloadAllComponents];
}

- (void)cancelButtonDown:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(cancelTransferCustomer)]) {
        [self.delegate cancelTransferCustomer];
    }
    [self hide];
}

- (void)sureButtonDown:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(sureTransferCustomer:)]) {
        ICServerModel *model = self.pickerArray[self.currDateRow];
        [self.delegate sureTransferCustomer:model.eId];
    }
    [self hide];
}

#pragma mark - Picker Datasource
//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

//列显示的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.pickerArray count];
}

#pragma mark - Picker Delegate

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 43.0f;
}

//当前行的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    ICServerModel *model = self.pickerArray[row];
    return model.eName;
}

//点击当前行的内容
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.currDateRow = row;
}

//修改Label字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    ICServerModel *model = self.pickerArray[row];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.width, 40)];
    label.text = model.eName;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = ICRGB(0x333333);
    label.font = ICFont(19);
    return label;
}

- (UIPickerView *)customerPicker {
    if (!_customerPicker) {
        _customerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.height - CustomerPickerViewHeight , self.width, CustomerPickerViewHeight)];
        _customerPicker.backgroundColor = [UIColor whiteColor];
        _customerPicker.delegate = self;
        _customerPicker.dataSource = self;
        [self addSubview:_customerPicker];
    }
    return _customerPicker;
}

-(UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 0, 50, self.height - CustomerPickerViewHeight)];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [_cancelButton setTitleColor:ICRGB(0x999999) forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelButton];
    }
    return _cancelButton;
}

-(UIButton *)sureAction {
    if (!_sureAction) {
        _sureAction = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 50 - 15, 0, 50, self.height - CustomerPickerViewHeight)];
        [_sureAction setTitle:@"确定" forState:UIControlStateNormal];
        _sureAction.titleLabel.font = [UIFont systemFontOfSize:17];
        [_sureAction setTitleColor:ICRGB(0x49a1d2) forState:UIControlStateNormal];
        [_sureAction addTarget:self action:@selector(sureButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_sureAction];
    }
    return _sureAction;
}

- (UILabel *)headView {
    if (!_headView) {
        _headView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - CustomerPickerViewHeight)];
        _headView.text = @"请选择客服人员";
        _headView.textColor = ICRGB(0x999999);
        _headView.layer.borderWidth = 0.5f;
        _headView.font = [UIFont systemFontOfSize:16];
        _headView.layer.borderColor = ICRGB(0xc8c7cc).CGColor;
        _headView.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_headView];
    }
    return _headView;
}

-(void)dealloc {
    NSLog(@"ICTransferCustomerView dealloc");
}

@end
